<template>
      <div style="width: 100%;height: 100vh;overflow: hidden">
          <div style="width: 400px;margin: 150px auto">
              <div style="color: #3b2f85;font-size: 30px;text-align: center;padding: 30px">欢迎登录xx教育平台</div>

              <el-form ref="form" :model="form" size="default" :rules="rules">
                  <el-form-item prop="username">
                      <el-input v-model="form.username" placeholder="请输入账号"/>
                  </el-form-item>
                  <el-form-item prop="password">
                      <el-input type="password" v-model="form.password" placeholder="请输入密码"/>
                  </el-form-item>
                  <el-form-item>
                      <el-button style="width: 100%;background-color:#72cdd5;" @click="login">登录</el-button>
                  </el-form-item>
<!--                  <el-form-item>-->
<!--                      <el-button style="width: 100%;background-color:#72cdd5;" @click="register">注册</el-button>-->
<!--                  </el-form-item>-->
              </el-form>


          </div>
      </div>
</template>

<script>

import request from "@/utils/request";
import {FormInstance} from "element-plus";
import {login} from "@/api/user";

export default {
  name: "Login",
  data(){
    return {
      form:{
          username:'user',
          password:'admin'
      },
      rules:{
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      }
    }
  },
  methods:{
    login(){
      this.$refs['form'].validate((valid)=>{
        if (valid) {

          // request.post("/user/login",this.form).then(res=>{
            login(this.form).then(res=>{
                console.log(res)
            if (res.code === 20000){
              //把用户信息存到session里面去
              sessionStorage.setItem("user",JSON.stringify(res.data))
              if (res.data.type === 0){ //学生
                this.$router.push("/course")  //登录成功之后跳转
              }else {
                  this.$router.push("/404")
              }
            }else {
              this.$message({
                type:"error",
                message:res.msg
              })
            }
          })

        }
      })
    },
    register(){
      request.post("/user/login",this.form).then(res=>{
      let a=true
      if(a=true){
        this.$router.push('/register')
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
